Imports Microsoft.VisualBasic.CompilerServices
Public Class clsSupport
    Public Shared Sub Fill2Grid(ByRef dv As DataView, ByRef grdFill As DataGrid, ByRef tbs As DataGridTableStyle, ByRef Cols As DataGridTextBoxColumn(), ByVal cFields As String, ByVal cHeaders As String, ByVal cFormat As String, ByVal cWidth As String)
        Dim grid As DataGrid = grdFill
        grid.TableStyles.Add(tbs)
        tbs.MappingName = dv.Table.ToString
        Dim num2 As Long = GetWordCount(cFields, ",")
        Dim i As Long = 1
        Do While (i <= num2)
            Dim str As String = Trim(GetWordNum(cFields, CInt(i), ","))
            Dim expression As String = GetWordNum(cFormat, CInt(i), ",")
            If (Strings.InStr(str, "~", CompareMethod.Binary) = 0) Then
                Dim column As New DataGridTextBoxColumn
                column.MappingName = str
                column.HeaderText = Trim(GetWordNum(cHeaders, CInt(i), ","))
                column.Format = expression
                column.NullText = ""
                AddHandler column.TextBox.KeyPress, AddressOf txtKeyPressEnter
                If (Strings.InStr(column.Format, "#", CompareMethod.Binary) > 0) Then
                    column.Alignment = HorizontalAlignment.Right
                    column.TextBox.TextAlign = HorizontalAlignment.Right
                    column.TextBox.Tag = (expression & "Out")
                    column.TextBox.MaxLength = Strings.Len(expression)
                    AddHandler column.TextBox.KeyPress, AddressOf txtNumericKeyPress
                    AddHandler column.TextBox.Enter, AddressOf txtNumeric_Enter
                    Try
                        dv.Table.Columns.Item(str).DefaultValue = 0
                        column.NullText = "0"
                    Catch exception1 As Exception
                    End Try
                End If
                'If (Strings.InStr(dv.Table.Columns.Item(str).DataType.ToString, "System.Date", CompareMethod.Binary) > 0) Then
                '    column.TextBox.MaxLength = 10
                '    column.NullText = clsSupport.GetEmptyDate
                'End If
                column.Width = CInt(Math.Round(Conversion.Val(Trim(GetWordNum(cWidth, CInt(i), ",")))))
                Cols(CInt((i - 1))) = column
                tbs.GridColumnStyles.Add(column)
            Else
                Dim column2 As New DataGridBoolColumn
                column2.FalseValue = False
                column2.TrueValue = True
                column2.AllowNull = False
                column2.NullValue = False
                column2.MappingName = Strings.Replace(str, "~", "", 1, -1, CompareMethod.Binary)
                column2.HeaderText = Trim(GetWordNum(cHeaders, CInt(i), ","))
                column2.Width = CInt(Math.Round(Conversion.Val(Trim(GetWordNum(cWidth, CInt(i), ",")))))
                column2.Alignment = HorizontalAlignment.Center
                tbs.GridColumnStyles.Add(column2)
            End If
            i = (i + 1)
        Loop
        grid.DataSource = dv
        grid = Nothing
    End Sub
    Public Shared Sub txtNumericKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        Dim tb As TextBox = CType(sender, TextBox)
        Dim chr As Char = e.KeyChar
        If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
            e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
        ElseIf e.KeyChar = "." Then
            If Not (tb.SelectedText = "." Or IsNumeric(tb.Text & e.KeyChar)) Then
                e.Handled = True
            End If
        ElseIf e.KeyChar = "-" Then
            If tb.SelectionStart <> 0 Or Microsoft.VisualBasic.Left(tb.Text, 1) = "-" Then
                e.Handled = True
            End If
        ElseIf Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub
    Public Shared Sub txtNumeric_Enter(ByVal sender As Object, ByVal e As System.EventArgs)
        sender.Text = Replace(sender.Text, " ", "")
    End Sub

#Region "Basic"
    Public Shared Function GetWordCount(ByVal cString As String, ByVal cDelimiter As Char) As Object
        Dim num As Integer = 0
        Dim length As Long = cString.Length
        Dim num3 As Long = 0
        If (cString.Chars(0) <> cDelimiter) Then
            num3 = (num3 + 1)
        End If
        Dim num4 As Integer = (num + 1)
        Dim num5 As Integer = CInt((length - 1))
        num = 0
        Do While (((num4 >> &H1F) Xor num) <= ((num4 >> &H1F) Xor num5))
            If (cString.Chars(num) = cDelimiter) Then
                Do
                    num += 1
                    If (num >= length) Then
                        Exit Do
                    End If
                    If (cString.Chars(num) <> cDelimiter) Then
                        num3 = (num3 + 1)
                        Exit Do
                    End If
                Loop While (1 <> 0)
            End If
            num = (num + num4)
        Loop
        Return num3
    End Function

    Public Shared Function GetWordNum(ByVal cString As String, ByVal nWordPosition As Integer, ByVal cDelimiter As Char) As String
        cString = cString.Trim + cDelimiter
        Dim num As Integer = At(cDelimiter, cString, (nWordPosition - 1))
        Dim num2 As Integer = At(cDelimiter, cString, nWordPosition)
        Return Strings.Mid(cString, (num + 1), ((num2 - 1) - num))
    End Function
    Public Shared Function At(ByVal cSearchFor As String, ByVal cSearchIn As String, ByVal nOccurence As Integer) As Integer
        Return _At(cSearchFor, cSearchIn, nOccurence, 1)
    End Function
    Private Shared Function _At(ByVal cSearchFor As String, ByVal cSearchIn As String, ByVal nOccurence As Integer, ByVal nMode As Integer) As Integer
        Dim num2 As Integer = 0
        Dim num3 As Integer = 0
        Dim startIndex As Integer = 0
        If (nMode = 1) Then
            startIndex = 0
        Else
            startIndex = cSearchIn.Length
        End If
        Dim num5 As Integer = (num2 + 1)
        Dim num6 As Integer = nOccurence
        num2 = 1
        Do While (((num5 >> &H1F) Xor num2) <= ((num5 >> &H1F) Xor num6))
            If (nMode = 1) Then
                startIndex = cSearchIn.IndexOf(cSearchFor, startIndex)
            Else
                startIndex = cSearchIn.LastIndexOf(cSearchFor, startIndex)
            End If
            If (startIndex < 0) Then
                Exit Do
            End If
            num3 += 1
            If (num3 = nOccurence) Then
                Return (startIndex + 1)
            End If
            If (nMode = 1) Then
                startIndex += 1
            Else
                startIndex -= 1
            End If
            num2 = (num2 + num5)
        Loop
        Return 0
    End Function
    Public Shared Sub txtKeyPressEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If AscW(e.KeyChar) = 13 Then
            SendKeys.Send("{Tab}")
        End If
    End Sub

    Public Shared Function GetEmptyDate() As String
        Return "  /  /    "
    End Function
#End Region
#Region "Valid Numeric"
    
#End Region
End Class
